/*
 * GroupControl.java
 *
 * Created on 03 January 2004, 08:08
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.node.group;

import com.velocityme.client.gui.ChangeDeltaPanel;
import com.velocityme.client.gui.access.GuiDisplayGroupAccessByUser;
import com.velocityme.client.gui.exception.ExceptionWindow;
import com.velocityme.client.utility.StatelessRemoteClientSessionUtil;
import com.velocityme.entity.PermissionBean;
import com.velocityme.interfaces.KeySession;
import com.velocityme.interfaces.GroupPK;
import com.velocityme.interfaces.NodePK;
import com.velocityme.interfaces.PermissionPK;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.PermissionDeniedException;
import com.velocityme.valueobjects.GroupValue;
import com.velocityme.valueobjects.UserValue;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import javax.ejb.CreateException;
import javax.naming.NamingException;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;

/**
 *
 * @author  Robert Crida Work
 */
public class GroupControl extends javax.swing.JPanel {
    
    private GroupPK m_groupPK;
    private NodePK m_parentNodePK;
    private KeySession m_key;
    private GroupValueDisplay m_groupValueDisplay;
    private boolean m_editing = true;
    private Set m_setPermissions;
    private Collection m_allUserValues = null;
    private Collection m_assignedUserValues;

    /** Creates new form UserControl */
    public GroupControl(KeySession p_key, GroupPK p_groupPK, boolean p_editing, Set p_setPermissions) {
        initComponents();
        m_key = p_key;
        m_groupPK = p_groupPK;
        m_editing = p_editing;
        m_setPermissions = p_setPermissions;
        try {
            if (p_editing)
                m_allUserValues = StatelessRemoteClientSessionUtil.getSession().getAllUserValues(m_key);
            GroupValue groupValue = StatelessRemoteClientSessionUtil.getSession().getGroupValue(m_key, m_groupPK);
            m_assignedUserValues = StatelessRemoteClientSessionUtil.getSession().getGroupUserValues(m_key, m_groupPK);
            m_groupValueDisplay = new GroupValueDisplay(groupValue, m_allUserValues, m_assignedUserValues);
            if (!p_editing) {
                jTabbedPane1.addTab("Change Log", new ChangeDeltaPanel(m_key, new NodePK(m_groupPK.getGroupId())));
                jTabbedPane1.addTab("Group Access", new GuiDisplayGroupAccessByUser (m_key, m_groupPK));
                jButtonEdit.setEnabled(m_setPermissions.contains(new PermissionPK(new Integer(PermissionBean.GROUP_EDIT))));
            }
            else {
                jButtonOK.setEnabled(true);
                jButtonCancel.setEnabled(true);
            }
            m_groupValueDisplay.setEditable(p_editing);
            jPanel2.add(m_groupValueDisplay, java.awt.BorderLayout.CENTER);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            JOptionPane.showMessageDialog(this, "Permission denied!", "View Group", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    /** Creates new form UserControl */
    public GroupControl(KeySession p_key, NodePK p_parentNodePK) {
        initComponents();
        m_key = p_key;
        m_parentNodePK = p_parentNodePK;
        try {
            m_allUserValues = StatelessRemoteClientSessionUtil.getSession().getAllUserValues(m_key);
            m_groupValueDisplay = new GroupValueDisplay(new GroupValue(), m_allUserValues, null);
            m_groupValueDisplay.setEditable(true);
            jPanel2.add(m_groupValueDisplay, java.awt.BorderLayout.NORTH);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
        jButtonOK.setEnabled(true);
        jButtonCancel.setEnabled(true);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jButtonEdit = new javax.swing.JButton();
        jButtonOK = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel2.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                jPanel2ComponentShown(evt);
            }
        });

        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        jButtonEdit.setText("Edit");
        jButtonEdit.setEnabled(false);
        jButtonEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditActionPerformed(evt);
            }
        });

        jPanel1.add(jButtonEdit);

        jButtonOK.setText("OK");
        jButtonOK.setEnabled(false);
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        jPanel1.add(jButtonOK);

        jButtonCancel.setText("Cancel");
        jButtonCancel.setEnabled(false);
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jPanel1.add(jButtonCancel);

        jPanel2.add(jPanel1, java.awt.BorderLayout.SOUTH);

        jTabbedPane1.addTab("Group", jPanel2);

        add(jTabbedPane1, java.awt.BorderLayout.CENTER);

    }//GEN-END:initComponents

    private void jPanel2ComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jPanel2ComponentShown
        // Add your handling code here:
        try {
            if (!m_editing)
                m_groupValueDisplay.setGroupValue(StatelessRemoteClientSessionUtil.getSession().getGroupValue(m_key, m_groupPK), m_allUserValues, m_assignedUserValues);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }        
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }        
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }        
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            JOptionPane.showMessageDialog(this, "Permission denied!", "View Group", JOptionPane.WARNING_MESSAGE);
        }        
    }//GEN-LAST:event_jPanel2ComponentShown

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        // Add your handling code here:
        getParent().remove(this);                
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
        // Add your handling code here:
        try {
            GroupValue groupValue = m_groupValueDisplay.getGroupValue();
            Collection userPKs = new ArrayList();
            Iterator i = m_groupValueDisplay.getUserValues().iterator();
            while(i.hasNext()) {
                userPKs.add(((UserValue)i.next()).getPrimaryKey());
            }
            if (groupValue.groupIdHasBeenSet())
                StatelessRemoteClientSessionUtil.getSession().groupEdit(m_key, m_groupPK, groupValue, userPKs);
            else {
                StatelessRemoteClientSessionUtil.getSession().groupCreate(m_key, m_parentNodePK, groupValue, userPKs);
            }
            getParent().remove(this);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            ExceptionWindow.showError(e);
        }        
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }        
    }//GEN-LAST:event_jButtonOKActionPerformed

    private void jButtonEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditActionPerformed
        // Add your handling code here:
        GroupControl editPanel = new GroupControl(m_key, m_groupPK, true, m_setPermissions);
        ((JTabbedPane)getParent()).addTab("Edit Group", editPanel);
        ((JTabbedPane)getParent()).setSelectedComponent(editPanel);                
    }//GEN-LAST:event_jButtonEditActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonEdit;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
    
}
